package cn.javaSong.service.impl;

import cn.javaSong.common.ResponseHelper;
import cn.javaSong.common.ResponseModel;
import cn.javaSong.entity.WyySong;
import cn.javaSong.mapper.WyySongMapper;
import cn.javaSong.payload.SelectSongName;
import cn.javaSong.payload.base.PageRequest;
import cn.javaSong.service.IWyySongService;
import cn.javaSong.utils.StringUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.io.FileUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.File;
import java.nio.file.Paths;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dakai
 * @since 2024-10-24
 */
@Service
public class WyySongServiceImpl extends ServiceImpl<WyySongMapper, WyySong> implements IWyySongService {

    @Value("${songFilePath}")
    private String songFilePath;

    @Value("${img-url}")
    private String imgUrl;

    @Resource
    private WyySongMapper wyySongMapper;

    @Override
    public ResponseModel addImg(WyySong song) {
        WyySong songs = new WyySong();
        songs.setSongName(song.getSongName());
        songs.setId(StringUtil.createUUID());
        songs.setLyricName(song.getLyricName());
        songs.setUpdateTime(new Date());
        songs.setUseName(song.getUseName());
        String imageUrl = song.getNames();
        String names = StringUtil.getCurrentDateStr();
        String imageName = names + ".png";
        String songName = names + ".mp3";
        songs.setImgName(imageName);
        songs.setLyricName(songName);
        File imageFile = new File(Paths.get(songFilePath, imageName).toString());
        File songFile = new File(Paths.get(songFilePath, songName).toString());
        RestTemplate restTemplate = new RestTemplate();
        byte[] imageBytes = restTemplate.getForObject(imageUrl, byte[].class);
        try {
            StringUtil.downloadMp3(song.getName(), String.valueOf(songFile));
            FileUtils.writeByteArrayToFile(imageFile, imageBytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<WyySong> wyySong = wyySongMapper.selectWyy();
        for (WyySong song1 : wyySong) {
            if (song.getSongName().equals(song1.getSongName()) && song.getUseName().equals(song1.getUseName())) {
                return ResponseHelper.responseModel(200,"数据已存在，添加失败","失败");
            }
        }
        this.save(songs);
        return ResponseHelper.responseModel(200,"添加成功","成功");
    }

    @Override
    public IPage<WyySong> selectSong(SelectSongName request) {
        IPage<WyySong> songIPage = wyySongMapper.selectSong(PageRequest.getPage(request), request.getSongName());
        for (WyySong wyySong : songIPage.getRecords()) {
            wyySong.setImgName(imgUrl + wyySong.getImgName());
            wyySong.setLyricName(imgUrl + wyySong.getLyricName());
        }
        return songIPage;
    }
}
